package org.hs.phd.odi.tracking.tracker.similaritymeasure;

import org.hs.phd.odi.tracking.tracker.Histogram;
import org.hs.phd.odi.tracking.tracker.SimilarityMeasure;
import org.hs.phd.odi.tracking.tracker.histogram.IncompatibleHistogramsException;

public class BhattacharyaCoefficientSimilartyMeasure implements SimilarityMeasure {

	@Override
	public float calculateSimilarity( Histogram histogram1, Histogram histogram2) {

		float result = 0;
		
		float[] targetHistogramData = histogram1.getData();
		float[] targetCandidateHistogramData = histogram2.getData();
		
		if( targetHistogramData.length != targetCandidateHistogramData.length){
			throw new IncompatibleHistogramsException();
		}
		
		for(int i = 0; i < targetHistogramData.length; i++){
			result += Math.sqrt(targetHistogramData[i]*targetCandidateHistogramData[i]);
		}
		
		return result;
	}
}
